package com.tms.crud;

import java.lang.reflect.Field;

import com.tms.warehouse.entity.Identificable;

public class Attribute {

	private Field field;
	Class<? extends Identificable> entityType;

	public Attribute(Field field, Class<? extends Identificable> entityType) {

		super();
		this.field = field;
		this.entityType = entityType;
	}

	public String getName() {

		return field.getName();
	}

	public String getEntityType() {

		return entityType.getSimpleName();
	}

	public Object get(Identificable entity) {
		try {
			field.setAccessible(true);
			return field.get(entity);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void set(Identificable entity, Object value) {
		try {
			field.setAccessible(true);
			field.set(entity, value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public String toString() {

		return getEntityType() + "." + getName();
	}
}
